package com.yiwei.zxp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.api.model.AddressModel;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class AreaRightAdapter extends ItemAdapter<AddressModel.DatasBean, AreaRightAdapter.DefaultHolder> {

    public AreaRightAdapter(Context context) {
        super(context);
    }
    private int selectPoi = -1;
    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_address_right, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        AddressModel.DatasBean item = getItem(position);
        holder.item = item;
        holder.poi = position;
        holder.txtCity.setText(item.name);
        if (selectPoi == position) {
            holder.txtCity.setTextColor(0xffFF0030);
        } else {
            holder.txtCity.setTextColor(0xff000000);
        }
    }

    class DefaultHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.txt_city)
        TextView txtCity;
        AddressModel.DatasBean item;
        private int poi;
        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            txtCity.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            selectPoi = poi;
            notifyDataSetChanged();
            if(onChooseListener!=null){
                onChooseListener.onAreaSelect(item.id,item.name);
            }
        }
    }


    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onAreaSelect(String id, String name);
    }

    @Override
    public void update(List<AddressModel.DatasBean> datasBeen) {
        selectPoi=-1;
        super.update(datasBeen);
    }
}
